import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/login.vue'
import home from '../components/home.vue'
import Welcome from '../components/welcome.vue'
import user from '../components/user/user.vue'
import right from '../components/power/right.vue'
import role from '../components/power/role.vue'
Vue.use(VueRouter)
const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: login },
    {
      path: '/home',
      component: home,
      redirect:'/Welcome',
       children: [
        { path: '/Welcome', component: Welcome },
        { path: '/users', component: user },
        { path: '/rights', component: right },
        { path: '/roles', component: role }
      
    ]
    }
  ]
})
router.beforeEach((to, from, next) => {
  const tokenstr = window.sessionStorage.getItem('token')
  /* 如果本地 存在 token 则 不允许直接跳转到 登录页面 */
  if (to.fullPath === '/login') return next()
  else {
    if (tokenstr === null) {
      next('/login')
    }
    next()
  }
})
export default router
